package com.hinotoyk.testsystem.uitls;
import java.text.ParsePosition;
import java.util.*;
import java.text.SimpleDateFormat;

public class TimeTool {
    // 初始化所有格式
    private static SimpleDateFormat datetime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat date=new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat time=new SimpleDateFormat("HH:mm:ss");
    private static SimpleDateFormat year=new SimpleDateFormat("yyyy");
    private static SimpleDateFormat timestamp=new SimpleDateFormat("yyyyMMdd HHmmss");

    private TimeTool() {}

    // 获取满足数据库date类型的当前时间字符串
    static public String GetSqlDate() {
        Calendar c=Calendar.getInstance();
        return date.format(c.getTime());
    }

    // 获取满足数据库time类型的当前时间字符串
    static public String GetSqlTime() {
        Calendar c=Calendar.getInstance();
        return time.format(c.getTime());
    }

    // 获取满足数据库datetime类型的当前时间字符串
    static public String GetSqlDateTime() {
        Calendar c=Calendar.getInstance();
        return datetime.format(c.getTime());
    }

    // 获取满足数据库year类型的当前时间字符串
    static public String GetSqlYear() {
        Calendar c=Calendar.getInstance();
        return year.format(c.getTime());
    }

    // 获取满足数据库timestamp类型的当前时间字符串
    static public String GetSqlTimeStamp() {
        Calendar c=Calendar.getInstance();
        return timestamp.format(c.getTime());
    }

    static public Long GetLongTime(){
        Calendar c=Calendar.getInstance();
        return c.getTimeInMillis();
    }

    static private Long ToLong(SimpleDateFormat fmt,String text){
        Date tmp=fmt.parse(text,new ParsePosition(0));
        Calendar c=Calendar.getInstance();
        c.setTime(tmp);
        return c.getTimeInMillis();
    }

    static public Long DateToLong(String text){
        return ToLong(date,text);
    }

    static public Long DateTimeToLong(String text){
        return ToLong(datetime,text);
    }

    static public Long TimeToLong(String text){
        return ToLong(time,text);
    }

    static public Long YearToLong(String text){
        return ToLong(year,text);
    }

    static public Long TimeStampToLong(String text){
        return ToLong(timestamp,text);
    }

    static private String ToString(SimpleDateFormat fmt,Long l){
        Calendar c=Calendar.getInstance();
        c.setTimeInMillis(l);
        return fmt.format(c.getTime());
    }

    static public String LongToDate(Long l){
        return ToString(date,l);
    }

    static public String LongToDateTime(Long l){
        return ToString(datetime,l);
    }

    static public String LongToTime(Long l){
        return ToString(time,l);
    }

    static public String LongToYear(Long l){
        return ToString(year,l);
    }

    static public String LongToTimeStamp(Long l){
        return ToString(timestamp,l);
    }
}
